package com.robusta.cityuate.base;

import java.io.Serializable;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;

import com.robusta.cityuate.GCMIntentService;

import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.Window;
import com.google.android.gcm.GCMRegistrar;
import com.robusta.cityuate.CityuateApplication;
import com.robusta.cityuate.R;
import com.robusta.cityuate.activities.LoginActivity;
import com.robusta.cityuate.base.ViewType.Type;
import com.robusta.cityuate.sideMenu.SlidingMenuFragment;
import com.robusta.cityuate.sideMenu.SlidingMenuFragment.SlidingMenuControllers;
import com.robusta.cityuate.utils.Token;
import com.slidingmenu.lib.SlidingMenu;
import com.slidingmenu.lib.app.SlidingFragmentActivity;

public class CityuateFragmentMenuActivity extends SlidingFragmentActivity
		implements SlidingMenuControllers {
	public ViewType.Type ownershipType;
	protected SlidingMenu sideMenu;

	BroadcastReceiver closeReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			finish();

		}
	};

	@Override
	public void onCreate(Bundle arg0) {

		GCMIntentService.register(this);

		if (Token.getToken(this) == null) {
			startActivity(new Intent(this, LoginActivity.class));
			finish();
		}
		getIntent().addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);

		super.onCreate(arg0);
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			Serializable t = extras.getSerializable(ViewType.VIEWTYPE);
			ownershipType = t != null ? ((ViewType.Type) t) : Type.OWNER;
		}
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		setSupportProgressBarIndeterminateVisibility(false);

		// set the Behind View
		setBehindContentView(R.layout.menu_frame);
		getSupportFragmentManager().beginTransaction()
				.replace(R.id.menu_frame, new SlidingMenuFragment()).commit();
		//
		// // customize the SlidingMenu
		// customize the SlidingMenu
		sideMenu = getSlidingMenu();
		// sm.setShadowWidthRes(R.dimen.shadow_width);
		// sm.setShadowDrawable(R.drawable.shadow);
		sideMenu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
		sideMenu.setFadeDegree(0.35f);
		sideMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		sideMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		registerReceiver(closeReceiver, new IntentFilter(
				CityuateApplication.CLOSE_TAG));

	}

	@Override
	protected void onStart() {
		super.onStart();
		setSupportProgressBarIndeterminateVisibility(false);
	}

	@Override
	protected void onPause() {
		super.onPause();
		setSupportProgressBarIndeterminateVisibility(false);
	}

	@Override
	public void hideMenu() {
		getSlidingMenu().toggle();
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			getSlidingMenu().toggle();
			break;

		default:
			break;
		}

		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		unregisterReceiver(closeReceiver);
	}

}
